<?php

namespace app\admin\service;

use app\common\model\trCarType;
use app\common\util\ErrorCode;

/**
 * Class Auth
 * @package app\admin\service
 * 车型服务类
 */
class CarTypeService
{
    private $carTypeField = [
        "name" => "require", "order_by" => "0", "seat" => "require", "icon" => "require", "remark" => ""
    ]; //填入数据库需要存储的表字段
    private $data = [];
    private $returnData;
    private $mall_id = 0;
    private $params;

    public function __construct($data, $params = [])
    {
        $this->returnData = $data;
        $this->params = $params;
        $this->mall_id = request()->mallData["id"];
    }

    // 获取所有可用的类型
    public function getChoose(){
        $model = new trCarType();
        $this->returnData["data"] = $model->getList("id,name", ["status" => 1, "mall_id" => $this->mall_id], 0,0, "order_by desc", false);
        return $this->returnData;
    }

    // 获取车辆类型数据的分页列表
    public function getList(){
        $page = isset($this->params['page']) ? $this->params['page'] : 1;
        $pageSize = isset($this->params['pageSize']) ? $this->params['pageSize'] : 10;
        //搜索条件
        $filter = array(["mall_id", "=", $this->mall_id]);
        if(isset($this->params['name'])){
            $filter[] = ['name', 'like', "%" . $this->params["name"] . "%"];
        }
        $model = new trCarType();
        $data = $model->getList("*", $filter, $page, $pageSize);
        if(!$data["data"]){
            $this->returnData['data'] = ['list' => [], 'totalPage' => $page, 'currentPage' => $page, 'count' => 0];
        }else {
            $this->returnData['data'] = ['list' => $data['data'], 'totalPage' => $data['last_page'], 'currentPage' => $data['current_page'], 'count' => $data['total']];
        }
        return $this->returnData;
    }

    // 添加车辆类型
    public function toAdd(){
        foreach ($this->carTypeField as $k => $v) {
            $ks = parse_name($k); //转换字段，变驼峰
            if($v === 'require' && !isset($this->params[$ks])){
                return ErrorCode::code("params_not_exist", $ks);
            }
            $this->data[$k] = isset($this->params[$ks]) ? $this->params[$ks] : $v;
        }
        $model = new trCarType();
        $result = $model->add($this->data);
        if(isset($result["code"])) {
            return $result;
        }
        if(!$result){
            $this->returnData = ErrorCode::$errorCode["fail"];
        }
        return $this->returnData;
    }

    // 更新车辆类型
    public function toUpdate(){
        if(empty($this->params['id'])){
            return ErrorCode::code("params_not_exist", "id");
        }

        foreach ($this->carTypeField as $k => $v) {
            $ks = parse_name($k); //转换字段，变驼峰
            if($v === 'require' && !isset($this->params[$ks])){
                return ErrorCode::code("params_not_exist", $ks);
            }
            if(isset($this->params[$ks])){
                $this->data[$k] = $this->params[$ks];
            }
        }

        $model = new trCarType();
        $result = $model->doUpdate($this->data, ["id" => $this->params["id"], "mall_id" => $this->mall_id]);
        if(isset($result["code"])) {
            return $result;
        }
        if(!$result){
            $this->returnData = ErrorCode::$errorCode["fail"];
        }
        return $this->returnData;
    }

    // 车辆类型的详细内容
    public function getDetail(){
        if(empty($this->params['id'])){
            return ErrorCode::code("params_not_exist", "id");
        }
        $model = new trCarType();
        $data = $model->getFields("*", ["id" => $this->params["id"], "mall_id" => $this->mall_id]);
        $this->returnData["data"] = $data;
        return $this->returnData;
    }

    // 更新车辆类型权重
    public function toUpdateOrderBy(){
        if(empty($this->params['id'])){
            return ErrorCode::code("params_not_exist", "id");
        }

        if(!isset($this->params["orderBy"])){
            return ErrorCode::code("params_not_exist", "orderBy");
        }
        $this->data["order_by"] = $this->params["orderBy"];

        $model = new trCarType();
        $result = $model->doUpdate($this->data, ["id" => $this->params["id"], "mall_id" => $this->mall_id]);
        if(isset($result["code"])) {
            return $result;
        }
        if(!$result){
            $this->returnData = ErrorCode::$errorCode["fail"];
        }
        return $this->returnData;
    }

    // 更新车辆类型状态
    public function toUpdateStatus(){
        if(empty($this->params['id'])){
            return ErrorCode::code("params_not_exist", "id");
        }

        if(!isset($this->params["status"])){
            return ErrorCode::code("params_not_exist", "status");
        }
        if(!in_array($this->params["status"], [1,2])){
            return ErrorCode::code("params_error", "status");
        }
        $this->data["status"] = $this->params["status"];

        $model = new trCarType();
        $result = $model->doUpdate($this->data, ["id" => $this->params["id"], "mall_id" => $this->mall_id]);
        if(isset($result["code"])) {
            return $result;
        }
        if(!$result){
            $this->returnData = ErrorCode::$errorCode["fail"];
        }
        return $this->returnData;
    }

    // 删除车辆类型
    public function toDel(){
        if(empty($this->params['id'])){
            return ErrorCode::code("params_not_exist", "id");
        }
        $model = new trCarType();
        $result = $model->delById($this->params['id']);
        if (isset($result["code"])) {
            return $result;
        }
        if (!$result) {
            $this->returnData = ErrorCode::$errorCode['fail'];
            $this->returnData["msg"] = "删除失败";
        }
        return $this->returnData;
    }
}